package com.globallogic.capacitaciones.hibernate.dao;

import java.lang.reflect.ParameterizedType;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public abstract class AbstractDAO<T> extends HibernateDaoSupport implements DAO<T>{
	
	private Class<T> entityClass;
	
	@Override
	public T get(Long id) {
		
		return getHibernateTemplate().load(entityClass, id);
	}

	@Override
	public Long save(T entity) {
		
		return (Long) getHibernateTemplate().save(entity);
	}

	@Override
	public void delete(T entity) {
		
		getHibernateTemplate().delete(entity);
		
	}

	@SuppressWarnings("unchecked")
	@Autowired
	public void initialize(SessionFactory sessionFactory){
		setSessionFactory(sessionFactory);
		this.entityClass = (Class<T>) ((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
	}
}
